#include "StdAfx.h"
#include "ResourceFactory.h"
#include "tinyxml.h"
#include "atlimage.h"
#ifndef TEST
const char * g_xml = "<Resource>"
    "<music>"
        "<wav>"
            "<p1 offset=\"0\" size=\"39002\" />"
            "<p2 offset=\"39002\" size=\"20336\" />"
            "<p3 offset=\"59338\" size=\"11564\" />"
            "<p4 offset=\"70902\" size=\"53864\" />"
        "</wav>"
        "<midi />"
    "</music>"
    "<png>"
        "<it name=\"it\">"
            "<p5 offset=\"124766\" size=\"5939\" />"
            "<p6 offset=\"130705\" size=\"6075\" />"
            "<p7 offset=\"136780\" size=\"5994\" />"
            "<p8 offset=\"142774\" size=\"5748\" />"
            "<p9 offset=\"148522\" size=\"6511\" />"
            "<p10 offset=\"155033\" size=\"5834\" />"
            "<p11 offset=\"160867\" size=\"5584\" />"
            "<p12 offset=\"166451\" size=\"5601\" />"
            "<p13 offset=\"172052\" size=\"5450\" />"
            "<p14 offset=\"177502\" size=\"6132\" />"
            "<p15 offset=\"183634\" size=\"6111\" />"
            "<p16 offset=\"189745\" size=\"6511\" />"
            "<p17 offset=\"196256\" size=\"5810\" />"
            "<p18 offset=\"202066\" size=\"5583\" />"
            "<p19 offset=\"207649\" size=\"5462\" />"
            "<p20 offset=\"213111\" size=\"6373\" />"
            "<p21 offset=\"219484\" size=\"6685\" />"
            "<p22 offset=\"226169\" size=\"5643\" />"
            "<p23 offset=\"231812\" size=\"5457\" />"
            "<p24 offset=\"237269\" size=\"6020\" />"
            "<p25 offset=\"243289\" size=\"5640\" />"
            "<p26 offset=\"248929\" size=\"5767\" />"
            "<p27 offset=\"254696\" size=\"7105\" />"
            "<p28 offset=\"261801\" size=\"5814\" />"
            "<p29 offset=\"267615\" size=\"6090\" />"
        "</it>"
        "<black name=\"black\">"
            "<p30 offset=\"273705\" size=\"5894\" />"
            "<p31 offset=\"279599\" size=\"5262\" />"
            "<p32 offset=\"284861\" size=\"6120\" />"
            "<p33 offset=\"290981\" size=\"5890\" />"
            "<p34 offset=\"296871\" size=\"6980\" />"
            "<p35 offset=\"303851\" size=\"5421\" />"
            "<p36 offset=\"309272\" size=\"5669\" />"
            "<p37 offset=\"314941\" size=\"4827\" />"
            "<p38 offset=\"319768\" size=\"5841\" />"
            "<p39 offset=\"325609\" size=\"5209\" />"
            "<p40 offset=\"330818\" size=\"5487\" />"
            "<p41 offset=\"336305\" size=\"5916\" />"
            "<p42 offset=\"342221\" size=\"5414\" />"
            "<p43 offset=\"347635\" size=\"5487\" />"
            "<p44 offset=\"353122\" size=\"5556\" />"
            "<p45 offset=\"358678\" size=\"4730\" />"
            "<p46 offset=\"363408\" size=\"5097\" />"
            "<p47 offset=\"368505\" size=\"5718\" />"
            "<p48 offset=\"374223\" size=\"4932\" />"
            "<p49 offset=\"379155\" size=\"5684\" />"
            "<p50 offset=\"384839\" size=\"5819\" />"
            "<p51 offset=\"390658\" size=\"5508\" />"
            "<p52 offset=\"396166\" size=\"5694\" />"
            "<p53 offset=\"401860\" size=\"5861\" />"
            "<p54 offset=\"407721\" size=\"4980\" />"
            "<p55 offset=\"412701\" size=\"6509\" />"
            "<p56 offset=\"419210\" size=\"6091\" />"
            "<p57 offset=\"425301\" size=\"5389\" />"
            "<p58 offset=\"430690\" size=\"5179\" />"
            "<p59 offset=\"435869\" size=\"6538\" />"
            "<p60 offset=\"442407\" size=\"6271\" />"
            "<p61 offset=\"448678\" size=\"6249\" />"
            "<p62 offset=\"454927\" size=\"4966\" />"
            "<p63 offset=\"459893\" size=\"5826\" />"
            "<p64 offset=\"465719\" size=\"5826\" />"
            "<p65 offset=\"471545\" size=\"5548\" />"
            "<p66 offset=\"477093\" size=\"5928\" />"
            "<p67 offset=\"483021\" size=\"5512\" />"
            "<p68 offset=\"488533\" size=\"6655\" />"
            "<p69 offset=\"495188\" size=\"5562\" />"
            "<p70 offset=\"500750\" size=\"6068\" />"
            "<p71 offset=\"506818\" size=\"6810\" />"
            "<p72 offset=\"513628\" size=\"5132\" />"
            "<p73 offset=\"518760\" size=\"5858\" />"
            "<p74 offset=\"524618\" size=\"7593\" />"
            "<p75 offset=\"532211\" size=\"5891\" />"
            "<p76 offset=\"538102\" size=\"5880\" />"
            "<p77 offset=\"543982\" size=\"6438\" />"
            "<p78 offset=\"550420\" size=\"5501\" />"
            "<p79 offset=\"555921\" size=\"5189\" />"
            "<p80 offset=\"561110\" size=\"6068\" />"
            "<p81 offset=\"567178\" size=\"5705\" />"
            "<p82 offset=\"572883\" size=\"5501\" />"
            "<p83 offset=\"578384\" size=\"5225\" />"
            "<p84 offset=\"583609\" size=\"5856\" />"
            "<p85 offset=\"589465\" size=\"6302\" />"
            "<p86 offset=\"595767\" size=\"6262\" />"
            "<p87 offset=\"602029\" size=\"5942\" />"
            "<p88 offset=\"607971\" size=\"6040\" />"
            "<p89 offset=\"614011\" size=\"6367\" />"
            "<p90 offset=\"620378\" size=\"6098\" />"
            "<p91 offset=\"626476\" size=\"6334\" />"
            "<p92 offset=\"632810\" size=\"5888\" />"
            "<p93 offset=\"638698\" size=\"5870\" />"
            "<p94 offset=\"644568\" size=\"5556\" />"
            "<p95 offset=\"650124\" size=\"6078\" />"
        "</black>"
    "</png>"
"</Resource>"
;
#endif

SINGLETON_IMPL(ResourceFactory)

ResourceFactory::ResourceFactory(void)
{
}

ResourceFactory::~ResourceFactory(void)
{
}
BOOL ResourceFactory::LoadFile(const CString&dir)
{
	return m_file.Open(dir,CFile::modeReadWrite|CFile::typeBinary);
}
void ResourceFactory::CloseFile()
{
	m_file.Close();
}
BOOL ResourceFactory::GetData(const char *item,
							  const char *category,
							 int number,
							 CArray<PngItem *> *img_list)
{
	TiXmlDocument doc;
#ifdef TEST
	if (!doc.LoadFile( "madeByHand.xml" )) {
		return FALSE;
	}
#else
	if (doc.Parse(g_xml))
	{
		return FALSE;
	}
#endif
	TiXmlElement * root = doc.RootElement();
	TiXmlHandle hRoot=TiXmlHandle(root);
	if (!root) {
		return FALSE;
	}
	TiXmlElement * png = root->FirstChildElement( item );
	if (!png) {
		return FALSE;
	}
	TiXmlElement * cate = png->FirstChildElement(category);
	if (!cate) {
		return FALSE;
	}
	int offset = 0;
	int size = 0;

	TiXmlElement *i = cate->FirstChildElement();
	
	for (i;i&&number>0;number--,i = i->NextSiblingElement()) {
		i->Attribute("offset",&offset);
		i->Attribute("size",&size);
		if (size <= 0) {
			continue;
		}
		BYTE* lpRsrc = new BYTE[size];
		m_file.Seek(offset,CFile::begin);
		if (m_file.Read(lpRsrc,size) == size) {

			PngItem *p =new PngItem;
			p->ptr = lpRsrc;
			p->size = size;
			img_list->Add(p);	
		} else {
			delete [] lpRsrc;
		}
		
	}

	return TRUE;
}
/*
BOOL ResourceFactory::GetPngCategory(CArray<CString>*list)
{
	TiXmlDocument doc;
#ifdef TEST
	if (!doc.LoadFile( "madeByHand.xml" )) {
		return FALSE;
	}
#else
	if (doc.Parse(g_xml))
	{
		return FALSE;
	}
#endif
	TiXmlElement * root = doc.RootElement();
	TiXmlHandle hRoot=TiXmlHandle(root);
	if (!root) {
		return FALSE;
	}
	TiXmlElement * png = root->FirstChildElement("png");
	if (!png) {
		return FALSE;
	}
	TiXmlElement *cate = png->FirstChildElement();

	CString name;
	for (cate;cate;cate = cate->NextSiblingElement()) {
		CStringA t = cate->Attribute("name");
		name = CA2W(t,CP_UTF8);
		list->Add(name);
	}
}*/
void ResourceFactory::LoadMemImage(void *pimg,void *pMemData,long len)
{
	CImage *img = (CImage *)pimg;
	HGLOBAL hGlobal  =  GlobalAlloc(GMEM_MOVEABLE, len);
	void *  pData  =  GlobalLock(hGlobal);
	memcpy(pData, pMemData, len);
	GlobalUnlock(hGlobal);

	IStream *  pStream  =  NULL;
	if  (CreateStreamOnHGlobal(hGlobal, TRUE,  & pStream)  ==  S_OK)
	{

		if (SUCCEEDED(img->Load(pStream)))
		{
			for(int i = 0; i < img->GetWidth(); i++)  
			{  
				for(int j = 0; j < img->GetHeight(); j++)  
				{  
					unsigned char* pucColor = reinterpret_cast<unsigned char *>(img->GetPixelAddress(i , j));  
					pucColor[0] = pucColor[0] * pucColor[3] / 255;  
					pucColor[1] = pucColor[1] * pucColor[3] / 255;  
					pucColor[2] = pucColor[2] * pucColor[3] / 255;  
				}  
			} 

		} 
		pStream -> Release();     
	} 
	GlobalFree(hGlobal);

} 